package com.yin.jia.liang.controller;

import com.yin.jia.liang.common.Config;
import com.qiniu.util.Auth;
import com.yin.jia.liang.entity.GongGao;
import com.yin.jia.liang.entity.Goods;
import com.yin.jia.liang.service.GongGaoService;
import com.yin.jia.liang.service.GoodsService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("")
public class IndexController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    GongGaoService gongGaoService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(Model model) {
        List<Goods> t1 = goodsService.findByType(1);
        List<Goods> t2 = goodsService.findByType(2);
        List<Goods> t3 = goodsService.findByType(3);
        List<Goods> t4 = goodsService.findByType(4);
        List<GongGao> gongGaos = gongGaoService.find();
        model.addAttribute("t1", t1);
        model.addAttribute("t2", t2);
        model.addAttribute("t3", t3);
        model.addAttribute("t4", t4);
        model.addAttribute("gongGaos", gongGaos);
        return "index";
    }

    @RequestMapping(value = "/Uptoken", method = RequestMethod.POST)
    @ResponseBody
    public String getUptoken(String name,ModelMap model) {
        System.out.println("uptoken方法:"+name);
        // 获取七牛的uptoken
        String upToken=getUpToken();
        Map<String, String> map = new HashMap<String, String>();
        JSONObject json;
        map.put("upToken", upToken);
        json = JSONObject.fromObject(map);
        String check_return=json.toString();
        System.out.println("check_return="+check_return);
        return check_return;
    }



    private String getUpToken() {
        // 密钥配置
        System.out.println("初始化上传变量");
        Auth auth = Auth.create(Config.ACCESS_KEY, Config.SECRET_KEY);
        return auth.uploadToken(Config.bucketname);
    }
}
